package com.itheima.da_te_bs;

import com.itheima.bean.Course;
import com.itheima.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;


public class Test01 {
    @Test
    public void selectAll() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisStudent.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);

        SqlSession sqlSession = build.openSession(true);

        StudentMappoer mapper = sqlSession.getMapper(StudentMappoer.class);

        List<Student> students = mapper.selectAll();

        for (Student student : students) {
            System.out.println(student.getId() + "," + student.getName() + "," + student.getAge());
            List<Course> courses = student.getCourses();
            for (Course cours : courses) {
                System.out.println("\t"+cours);
            }
        }
        resourceAsStream.close();
        sqlSession.close();
    }
}
